<?php
	/**
	 * 标签库类
	 * 实现模板中定义的标签解析,数据提取,变量赋值等
	 * 目前支持的库类有taglib,widget
	 * @todo
	 * @author swingcoder <swingcoder@msn.com>
	 * @version 0.1
	 * @copyright Copyright (c) 2009, swingcoder.com
	 */

	class taglib
	{
		var $database = null;

		/**
		 * 构造函数
		 * detail description
		 * @return undefined
		 */
		function taglib()
		{
			$this->tag_file_root = config( 'dir.taglib' );
		}

		/**
		 * 根据定义获取数据
		 * detail description
		 * @param string $data
		 * @return array
		 */
		function result( $data )
		{
			$result = array( 'success' => false, 'data' => array(), 'message' => '' );

			$result = $this->parse( $data );

			if ( $result['success'] === true )
			{
				$tmp = $result['data'];
				$tabobj = load( 'biz.taglib.' . $tmp['class'] );
				$obj = new $tabobj();
				$result['data'] = $obj->run( $tmp['argument'] );
			}

			return $result;
		}

		/**
		 * 结构分析
		 * detail description
		 * @param string tag_value
		 * @return array
		 */
		function parse( $tag )
		{
			$result = array( 'success' => false, 'data' => array(), 'message' => '' );

			$tag_struc = json_decode( stripslashes( urldecode( $tag ) ), true );

			if ( is_array( $tag_struc ) )
			{
				$tag_class_file = $this->tag_file_root . $tag_struc['tagtype'] . '.php';
				if ( ! is_file( $tag_class_file ) )
				{
					$result['message'] = 'tag class file ' . $tag_class_file . ' not exists';
					return $result;
				}

				$result['success'] = true;
				$result['data']['var_name'] = $tag_struc['var_name'];
				$result['data']['class'] = $tag_struc['tagtype'];
				$result['data']['argument'] = $tag_struc['argument'];
			}

			return $result;
		}

	}
?>